<?php
/**
 * 功能: PhpStorm.
 * User: 文丹
 * Date: 2018/5/17 0017
 * Time: 9:25
 */

namespace app\index\model;
use think\Db;
use think\Model;

class User extends Model{
    /**
     *  登录验证
     * @param $password
     * @param $phone
     */
    public function findUser($password,$phone){
        $result = Db::name('user')
            ->where('password',$password)
            ->where('phone',$phone)
            ->find();
        return $result;
    }
    /**
     * 登录 token添加
     */
    public function addToken($data,$user_id){
        $result = Db::name('user')
            ->where('id',$user_id)
            ->update($data);
        return $result;
    }

    /**
     * 验证
     */
    public function find($phone){
        $result = Db::name('user')
            ->where('phone',$phone)
            ->find();
        return $result;
    }
    /**
     * 注册
     */
    public function add($data){
        $result = Db::name('user')
            ->insertGetId($data);
        return $result;
    }
    /**
     * 忘记密码
     */
    public function save_user($phone,$data){
        $result = Db::name('user')
            ->where('phone',$phone)
            ->update($data);
        return $result;
    }
    /**
     * 获得token
     */
    public function getToken($user_id){
        $result = Db::name('user')
            ->where('id',$user_id)
            ->find();
        return $result;
    }
    /**
     * 退出，清空token
     */
    public function saveToken($data,$user_id){
        $result = Db::name('user')
            ->where('id',$user_id)
            ->update($data);
        return $result;
    }
    /**
     * 获得 user
     */
    public function getUser($token){
        $result = Db::name('user')
            ->where('token',$token)
            ->find();
        if($result){
             $pid = Db::name('pid')->where('user_id',$result['id'])->value('adzone_pid');
            if($pid){
                $result['pid'] = $pid;
            }else{
                $result['pid'] = '';
            }
        }
        return $result;
    }

    /**
     * 获得user_id
     * @param $token
     * @return mixed
     */
    public function getUserId($token){
        $result = Db::name('user')
            ->where('token',$token)
            ->value('id');
        return $result;
    }

    /**
     * 完善个人资料
     * @param $data
     * @param $user_id
     */
    public function saveUser($data,$user_id){
        $result = Db::name('user')->where('id',$user_id)->update($data);
        return $result;
    }

    /**
     * 检查 token
     */
    public function checkToken($token){
        $result = Db::name('user')
            ->where('token',$token)
            ->find();
        return $result;
    }

    public function gerUserId($token){
        $result = Db::name('user')
            ->where('token',$token)
            ->value('id');
        return $result;
    }

    /**
     * 用户付款笔数
     * @param $token
     */
    public function getPay($token,$BeginDate,$endDate){
        $user = Db::name('user')
            ->where('token',$token)
            ->find();
        $user_id = $user['id'];
        //付款笔数
        $sum =  Db::name('goods_order')
            ->where('user_id',$user_id)
            ->where('state',1)
            ->whereTime('create_time','>=',$BeginDate)
            ->whereTime('create_time','<=',$endDate)
            ->count();

        if($user['status'] == 1){
            //普通用户
            $pay = $sum;
        }else{
            //合伙人
            $son = Db::name('user')->where('parent_id',$user_id)->select();
            // dump($son);
            $son_total2 = 0;
            if($son){
                $son_ids = [];
                foreach($son as $k=>$v){
                    $son_ids[] = $v['id'];
                    $son2 = Db::name('user')->where('parent_id',$v['id'])->select();
                    if($son2){
                        $son2_ids = [];
                        foreach($son2 as $k1=>$v1){
                            $son2_ids[] = $v1['id'];
                        }
                        $son_total2 =  Db::name('goods_order')
                            ->whereIn('user_id',$son2_ids)
                            ->where('state',1)
                            ->whereTime('create_time','>=',$BeginDate)
                            ->whereTime('create_time','<=',$endDate)
                            ->count();
                    }else{
                        $son_total2 = 0;
                    }
                }
                //第一批用户返利给平台
                $son_total = Db::name('goods_order')
                    ->whereIn('user_id',$son_ids)
                    ->where('state',1)
                    ->whereTime('create_time','>=',$BeginDate)
                    ->whereTime('create_time','<=',$endDate)
                    ->count();

                $pay = $sum+$son_total+$son_total2;
            }else{
                $pay = $sum;
            }

        }
        return $pay;
    }

    /**
     * 预估佣金
     * @param $token
     */
    public function getYj($token,$BeginDate,$endDate){

        $user = Db::name('user')
            ->where('token',$token)
            ->find();
        $user_id = $user['id'];

        $sum =  Db::name('goods_order')
            ->where('user_id',$user_id)
            ->whereTime('create_time','>=',$BeginDate)
            ->whereTime('create_time','<=',$endDate)
            ->where('state',1)
            ->sum('member_expect');

        $rule = Db::name('rebate_rule')->where('us_id',$user['status'])->find();
        $level1 = $rule['level1']/100;
        $level2 = $rule['level2']/100;

        $t = round($sum,2);//保留2位有效数
        $son = Db::name('user')->where('parent_id',$user_id)->select();
        if($son){
            $son_ids = [];
            foreach($son as $k=>$v){
                $son_ids[] = $v['id'];
            }
            //第一批用户返利给平台
            $son_total =  Db::name('goods_order')
                ->whereIn('user_id',$son_ids)
                ->whereTime('create_time','>=',$BeginDate)
                ->whereTime('create_time','<=',$endDate)
                ->where('state',1)
                ->sum('member_expect');

            $t1 = round($son_total*$level1,2);

            $son2 = Db::name('user')->whereIn('parent_id',$son_ids)->select();
            if($son2){
                $son2_ids = [];
                foreach($son2 as $k=>$v){
                    $son2_ids[] = $v['id'];
                }
                $son_total2 =  Db::name('goods_order')
                    ->whereIn('user_id',$son2_ids)
                    ->whereTime('create_time','>=',$BeginDate)
                    ->whereTime('create_time','<=',$endDate)
                    ->where('state',1)
                    ->sum('member_expect');

                $t2 =  round($son_total2*$level2,2);
            }else{
                $t2 = 0;
            }
            $yj = $t+$t1+$t2;
        }else{
            $yj = $t;
        }
        return $yj;
    }

    /**
     * 结算明细
     * @param $user_id
     * @param $begin
     * @param $end
     */
    public function getSettle($token,$begin,$end){
        $user    = $this->getUser($token);
        $user_id = $user['id'];
        $settle = Db::name('goods_order')
                ->where('user_id',$user_id)
                ->where('state',2)
                ->whereTime('finish_time','>=',$begin)
                ->whereTime('finish_time','<=',$end)
                ->field('goods_id,info,create_time,finish_time,order_num,pay,yj_sum,member_actual,member_expect')
                ->select();
        return $settle;


    }

    /**
     * 未结算
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getUnsettled($token,$begin,$end){
        $user    = $this->getUser($token);
        $user_id = $user['id'];
        $settle = Db::name('goods_order')
                ->where('user_id',$user_id)
                ->whereNotIn('state',2)
                ->whereTime('create_time','>=',$begin)
                ->whereTime('create_time','<=',$end)
              ->field('goods_id,info,create_time,finish_time,order_num,pay,yj_sum,member_actual,member_expect')
            ->select();
        return $settle;
    }

    /**
     * 上月/本月预估佣金
     * @param $sbegin
     * @param $sbegin
     * @param $user_id
     */
    public function get_last_predict($sbegin,$send,$user_id,$level1,$level2,$user){
        $last = Db::name('goods_order')
            ->where('user_id',$user_id)
            ->where('state','>',0)
            ->whereTime('create_time','>=',$sbegin)
            ->whereTime('create_time','<=',$send)
            ->sum('member_expect');

        if($user['status'] == 1){
            //有效粉丝
            $last_predict = round($last,2);//保留2位有效数
        }else{
            //合伙人
            $t = round($last,2);//保留2位有效数
            $son = Db::name('user')->where('parent_id',$user_id)->select();
            if($son){
                $son_ids = [];
                foreach($son as $k=>$v){
                    $son_ids[] = $v['id'];
                }
                //第一批用户返利给平台
                $son_total =  Db::name('goods_order')
                    ->whereIn('user_id',$son_ids)
                    ->where('state','>',0)
                    ->whereTime('create_time','>=',$sbegin)
                    ->whereTime('create_time','<=',$send)
                    ->sum('member_expect');
                $t1 = round($son_total*$level1,2);

                $son2 = Db::name('user')->whereIn('parent_id',$son_ids)->select();
                if($son2){
                    $son2_ids = [];
                    foreach($son2 as $k=>$v){
                        $son2_ids[] = $v['id'];
                    }
                    $son_total2 =  Db::name('goods_order')
                        ->whereIn('user_id',$son2_ids)
                        ->where('state','>',0)
                        ->whereTime('create_time','>=',$sbegin)
                        ->whereTime('create_time','<=',$send)
                        ->sum('member_expect');
                    $t2 =  round($son_total2*$level2,2);
                }else{
                    $t2 = 0;
                }
                $last_predict = $t+$t1+$t2;
            }else{
                $last_predict = $t;
            }

        }
        return $last_predict;
    }

    /**
     * 上月结算佣金
     * @param $sbegin
     * @param $send
     * @param $user_id
     * @param $level1
     * @param $level2
     */
    public function get_settle($sbegin,$send,$user_id,$level1,$level2,$user){
        $settle = Db::name('goods_order')
            ->where('user_id',$user_id)
            ->where('state',2)
            ->whereTime('finish_time','>=',$sbegin)
            ->whereTime('finish_time','<=',$send)
            ->sum('member_actual');
        $total = 0;
        if($user['status'] == 2){
            //有效粉丝
            $total = round($settle,2);//保留2位有效数
        }elseif($user['status'] == 3 ){
            //初级合伙人
            $t = round($settle,2);//保留2位有效数
            $son = Db::name('user')->where('parent_id',$user_id)->select();
            if($son){
                $son_ids = [];
                foreach($son as $k=>$v){
                    $son_ids[] = $v['id'];
                }
                //第一批用户返利给平台
                $son_total =  Db::name('goods_order')
                    ->whereIn('user_id',$son_ids)
                    ->where('state',2)
                    ->whereTime('finish_time','>=',$sbegin)
                    ->whereTime('finish_time','<=',$send)
                    ->sum('member_actual');
                $t1 = round($son_total*$level1,2);

                $son2 = Db::name('user')->whereIn('parent_id',$son_ids)->select();
                if($son2){
                    $son2_ids = [];
                    foreach($son2 as $k=>$v){
                        $son2_ids[] = $v['id'];
                    }
                    $son_total2 =  Db::name('goods_order')
                        ->whereIn('user_id',$son2_ids)
                        ->where('state',2)
                        ->whereTime('finish_time','>=',$sbegin)
                        ->whereTime('finish_time','<=',$send)
                        ->sum('member_actual');;
                    $t2 =  round($son_total2*$level2,2);
                }else{
                    $t2 = 0;
                }
                $total = $t+$t1+$t2;
            }else{
                $total = $t;
            }

        }elseif($user['status'] == 4){
            //高级合伙人

            $t = round($settle,2);//保留2位有效数
            $son = Db::name('user')->where('parent_id',$user_id)->select();
            if($son){
                $son_ids = [];
                foreach($son as $k=>$v){
                    $son_ids[] = $v['id'];
                }
                //第一批用户返利给平台
                $son_total =  Db::name('goods_order')
                    ->whereIn('user_id',$son_ids)
                    ->where('state',2)
                    ->whereTime('finish_time','>=',$sbegin)
                    ->whereTime('finish_time','<=',$send)
                    ->sum('member_actual');
                $t1 = round($son_total*$level1,2);

                $son2 = Db::name('user')->whereIn('parent_id',$son_ids)->select();
                if($son2){
                    $son2_ids = [];
                    foreach($son2 as $k=>$v){
                        $son2_ids[] = $v['id'];
                    }
                    $son_total2 =  Db::name('goods_order')
                        ->whereIn('user_id',$son2_ids)
                        ->where('state',2)
                        ->whereTime('finish_time','>=',$sbegin)
                        ->whereTime('finish_time','<=',$send)
                        ->sum('member_actual');
                    $t2 =  round($son_total2*$level2,2);
                }else{
                    $t2 = 0;
                }
                $total = $t+$t1+$t2;
            }else{
                $total = $t;
            }
            $senior = Db::name('senior')->find();//高级合伙人培养表
            $parent = Db::name('user')->where('id',$user_id)->value('parent_id');
            if($parent){

                if($total>0){
                    $check = Db::name('profit')
                        ->where('resoure_id',$user_id)
                        ->where('user_id',$parent)
                        ->where('time',$send)
                        ->find();
                    if(empty($check)){
                        $num = round($total*$senior['bonus'],2);//分红
                        Db::name('user')->where('id',$parent)->setInc('commission',$num);
                        Db::name('user')->where('id',$parent)->setInc('total',$num);
                        $arr = [
                            'user_id' => $parent,
                            'resoure_id' => $user_id,
                            'commission' => $num,
                            'time' => $send,
                            'state' => 1,//1收入，2支出
                            'type' => 6,//1自己订单收入，2粉丝提成收入，3用户提现支出，4维权订单收入，5粉丝维权订单收入,6培养高级合伙人收入
                            'remark' => '高级合伙人《' . $user['phone'] . '》月收益,平台分红' .$num.'(此分红是公司额外支付)'
                        ];
                        Db::name('profit')->insert($arr);
                    }

                }

            }

        }
        return $total;
    }







}